package com.kitty.mina.task;

import com.kitty.game.role.model.Role;
import com.kitty.mina.cache.SessionUtils;
import org.apache.mina.core.session.IoSession;
//后加
import com.kitty.mina.task.AbstractDistributeTask;
import com.kitty.mina.task.TaskHandlerContext;

public class ThreadLocalUtil {

    public static void addLocalTask(IoSession session, Runnable task) {
        if (session == null) {
            throw new NullPointerException("session is null");
        }
        Role role = SessionUtils.getRoleBySession(session);
        addLocalTask(role, task);
    }

    public static void addLocalTask(Role role, Runnable task) {
        if (role == null) {
            throw new NullPointerException("role is null");
        }
        TaskHandlerContext.INSTANCE.acceptTask(new AbstractDistributeTask() {
            int distributeKey = role.getDistributeKey();

            @Override
            public int distributeKey() {
                return distributeKey;
            }

            @Override
            public void action() {
                task.run();
            }
        });
    }

}
